﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Facebook
{
    public class FacebookParameters
    {
        public IDictionary<String, Object> SerializeToDictionary(ApplicationType applicationType)
        {
            var type = this.GetType();
            PropertyInfo[] properties = type.GetProperties();

            var nameDelegate = new Func<PropertyInfo, String>(propInfo =>
            {
                var attributes = propInfo.GetCustomAttributes(typeof(FacebookMethodParameterAttribute), true);
                if (attributes.Length == 0) return propInfo.Name;
                else
                {
                    var paramAttribute = (FacebookMethodParameterAttribute)attributes[0];
                    Object value;
                    Exception ex;
                    if (!paramAttribute.EvaluateValueIsValid(applicationType, propInfo, this, out value, out ex))
                        return paramAttribute.ParameterName;
                    else return null;
                }
            });

            var result = properties.ToDictionary(nameDelegate, propInfo => propInfo.GetValue(this, null));
            return result;
        }
    }
}
